package top.mczhengyi.learn.hello.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import top.mczhengyi.learn.hello.bean.Car;
import top.mczhengyi.learn.hello.bean.Pet;
import top.mczhengyi.learn.hello.bean.User;

@Configuration(proxyBeanMethods = true) // 告诉SpringBoot这是一个配置类， 相当于Spring中的xml配置文件
@ImportResource("classpath:beans.xml")
@EnableConfigurationProperties(Car.class)
// 1.开启Car的配置绑定功能
// 2.将Car组件自动注入到容器中
public class MyConfig {
    // 如果容器中包含pet01则向组件中添加Bean，否则不添加
    @ConditionalOnBean(name = "pet01")
    @Bean // 向容器中添加组件，以方法名作为ID， 返回值类型作为组件类型，返回的值为容器中的实例
    public User user01() {
        Pet pet = pet01();
        return new User("ZhangSan", 18, pet);
    }

    public Pet pet01() {
        return new Pet("Doge");
    }
}
